// -*- Mode: Java -*-
/* Copyright (c) 2006-2016, Universities Space Research Association (USRA).
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Universities Space Research Association nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY USRA ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL USRA BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

Command print(...);
Command test_udp_msg(...);
Command ack_msg(String);
Command quit_msg(String);
Real Lookup time;

test_send:
UncheckedSequence
{
    String arg1 = "CA";                    // intentionally shorter than declared in udp.xml (for testing)
    //Boolean arg2 = false;                // set to "true" to request an "ack" from the other agent
    Integer arg3 = 3;                      // 23 bits: -2147483649, 2147483648; 16 bits: -32768, 32767
    Real arg4 = 3.14159;                   // 1.17e-38, 3.5e+38
    Integer arg5[3] = #(9 8 7);            // test arrays
    Real arg6[3] = #(36.0 -104.7 25000.0);
    Boolean arg7[3] = #(true true false);
    String arg8[3] = #("foo" "baz" "bar");

    Boolean ackReceived;
    Boolean ackTimedOut;

    SendUdpCommand1:
    {
	ackReceived = false;
	ackTimedOut = false;
	print ("\ntest_send: sending arg1==", arg1, ", arg2==false, arg3==", arg3, ", arg4==", arg4,
	       ", arg5==", arg5, ", arg6==", arg6, ", arg7==", arg7, ", arg8==", arg8, "\n\n");
	test_udp_msg (arg1, false, arg3, arg4, arg5, arg6, arg7, arg8); // Send the UDP message, no ack
    }

    WaitForAckOrTimeout1:
    Concurrence
    {
	InvariantCondition !ackTimedOut;

        HandleAck1:
	OnCommand "ack_msg" (String ack)
            {
                print ("\ntest_send: received ack\n\n");
		ackReceived = true;
            }

        HandleTimeout1:
        UncheckedSequence
        {
            StartCondition Lookup(time, 1) > WaitForAckOrTimeout1.EXECUTING.START + 4;
            SkipCondition ackReceived;

	    print ("\ntest_send: ack timed out\n\n");

            SignalTimeout:
	    ackTimedOut = true;
        }
    }

    SendUdpCommand2:
    {
	ackReceived = false;
	ackTimedOut = false;
	print ("\ntest_send: sending arg1==", arg1, ", arg2==true, arg3==", arg3, ", arg4==", arg4,
	       ", arg5==", arg5, ", arg6==", arg6, ", arg7==", arg7, ", arg8==", arg8, "\n\n");
	test_udp_msg (arg1, true, arg3, arg4, arg5, arg6, arg7, arg8); // Send the UDP message, ack requested
    }

    WaitForAckOrTimeout2:
    Concurrence
    {
	InvariantCondition !ackTimedOut;

        HandleAck2:
	OnCommand "ack_msg" (String ack)
            {
                print ("\ntest_send: received ack\n\n");
		ackReceived = true;
            }

        HandleTimeout2:
        UncheckedSequence
        {
            StartCondition Lookup(time, 1) > WaitForAckOrTimeout2.EXECUTING.START + 4;
            SkipCondition ackReceived;

	    print ("\ntest_send: ack timed out\n\n");

            SignalTimeout2:
	    ackTimedOut = true;
        }
    }

    SendQuitCommand:
    {
	print ("\ntest_send: sending quit command\n\n");
	quit_msg ("quit");   // Send a Quit command
    }
}

// EOF
